﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SponsorshipManager.Database;

namespace SponsorshipManager.BusinessLayer
{
    public class SponsorDTO : PersonDTO
    {
        private bool _NeedsToSave;
        public enum StateEnum {NonParticipant,Participant,ExParticipant}
        public StateEnum State{get; set;}
        public int SponsorId{get; set;}
        public string CodiceFiscale { get; set; }
        public string introducedBy { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public List<Relationship> Relationships { get; private set; }

        /// <summary>
        /// Has unsaved changes.
        /// </summary>
        public bool NeedsToSave{
            get {
                return _NeedsToSave ;
            }
            set {
                _NeedsToSave = value;
            }
        }

        /// <summary>
        /// Load
        /// </summary>
        /// <returns></returns>
        public override bool Load()
        {
            StoredProcedure sp = DatabaseAdapter.Database.StoredProcedure();
            sp.Name = "uspGetSponsor";
            sp.AddParameter("SponsorId", DbType.Int32, SponsorId);
            sp.Token = User.CurrentUser.Token;
            int ret = sp.Execute();

            if (ret == 1)
            {
                DataRow dr = sp.QueryResult.Rows[0];
                LoadDataRow(dr);
            }
            return ret > 0 && base.Load();
        }

        public bool LoadDataRow(DataRow dr)
        {
            string s;
            int i;
            bool ret;
            DateTime d;
            try
            {

                ret = base.LoadDataRow(dr,DomainPerson);

                if (ret)
                {
                    s = dr["introducedBy"].ToString();
                    if (!String.IsNullOrEmpty(s))
                        introducedBy = s;

                    s = dr["state"].ToString();
                    if(Int32.TryParse(s,out i))
                        State = (StateEnum)i;
                    
                    
                    s =dr["StartDate"].ToString();
                    if (DateTime.TryParse(s, out d))
                    {
                        StartDate = d;
                    }
                    else
                    {
                        StartDate = DateTime.MinValue;
                    }

                    s = dr["EndDate"].ToString();
                    if (DateTime.TryParse(s, out d))
                    {
                        EndDate = d;
                    }
                    else
                    {
                        EndDate = DateTime.MaxValue;
                    }
                }

            }
            catch
            {
                ret = false;
            }
            return ret;
        }


        public static List<SponsorDTO> GetSponsorsPaged(StateEnum State, int PageNumber, int PageSize, out int TotalSponsors) 
        {
            List<SponsorDTO> Sponsors = new List<SponsorDTO>();
            int rows=0;
            StoredProcedure sp = DatabaseAdapter.Database.StoredProcedure();
            sp.Name = "uspGetSponsorsPaged";
            sp.AddParameter("State", DbType.Int32, 1, ParameterDirection.Input);
            sp.AddParameter("PageNumber", DbType.Int32, PageNumber, ParameterDirection.Input);
            sp.AddParameter("PageSize", DbType.Int32, PageSize, ParameterDirection.Input);
            sp.AddParameter("Rows", DbType.Int32, rows, ParameterDirection.Output);
            sp.Token = User.CurrentUser.Token;
            try
            {
                int ret = sp.Execute();
                TotalSponsors = (int)sp.GetParameter("Rows").Value;
                DataTable dt = sp.QueryResult;
                foreach (DataRow dr in dt.Rows)
                {
                    SponsorDTO s = new SponsorDTO();
                    if (s.LoadDataRow(dr))
                    {
                        Sponsors.Add(s);
                    }
                }

            }
            catch
            {
                TotalSponsors = 0;
            }
            return Sponsors;
        }

    }
}
